package gx.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import gx.entity.Manager;
import gx.service.ManagerService;

/**
	 * 管理员控制层
	 */
@Controller
@RequestMapping("/mng/mgr")
public class ManagerController {

		@Autowired // 自动初始化
		private ManagerService managerService;

		// 查询列表
		@RequestMapping("/list.jhtml")
		public ModelAndView list() {
			ModelAndView mav = new ModelAndView("mng/mgr/list");

			List<Manager> list = managerService.findAll();
			mav.addObject("list", list);
			return mav;
		}
		
		// 添加
		@RequestMapping("/add.jhtml")
		public String add(Manager manager) {
			managerService.save(manager);
			return "redirect:/mng/mgr/list.jhtml";
		}

		// 删除
		@RequestMapping("/delete.jhtml")
		public String delete(Integer id) {
			managerService.delete(id);
			// 重定向
			return "redirect:/mng/mgr/list.jhtml";
		}

		// 根据ID查看详情
		@RequestMapping("/detail.jhtml")
		public ModelAndView detail(Integer id) {
			ModelAndView mav = new ModelAndView("/mng/mgr/detail");
			Manager manager = managerService.get(id);
			mav.addObject("mgr", manager);
			return mav;
		}

		// 根据ID修改
		@RequestMapping("/edit.jhtml")
		public String edit(Integer id, String mobile, String email, Integer status) {
			// 查找一条
			Manager manager = managerService.get(id);
			// 修改
			
			manager.setEmail(email);
			manager.setStatus(status);
			managerService.update(manager);

			// 重定向
			return "redirect:/mng/mgr/list.jhtml";
		}

		// 根据用户名查询
		@RequestMapping("/findname.jhtml")
		public ModelAndView findByName(String condition) {
			ModelAndView mav = new ModelAndView("/mng/mgr/list");
			List<Manager> list = managerService.findByName(condition);
			mav.addObject("list", list);
			return mav;
		}
		
		//修改密码
		@RequestMapping("/pwd.jhtml")
		public String getPwd(String oldpwd, String newpwd, String newpwd2,Integer id,
				HttpServletRequest request) {
			String result = "/mng/pwd";
			Manager manager = managerService.get(id);
			if (manager.getPwd().equals(oldpwd)) { // 原密码对比
				if(newpwd==newpwd2){
				manager.setPwd(newpwd);
				managerService.update(manager);
				result = "redirect:/login.jsp";
				}
				else{
					request.setAttribute("msg", "新密码两次输入不一致！");
				}
			} else {
				request.setAttribute("msg", "原密码的错误，请检查！");
			}
			return result;
		}

		//退出
		@RequestMapping("/logout.jhtml")
		public String logout(HttpSession session) {
			session.invalidate(); // 失效
			return "redirect:/login.jsp";
		}
		
		@RequestMapping(value = "/ajaxdelete.jhtml", produces = "application/json;charset=UTF-8")
		@ResponseBody
		public String ajaxList(Integer id) throws Exception {
			String rs = "";
			try {
				managerService.delete(id);
				rs = "删除成功！";
			} catch (Exception e) {
				rs = "该管理员已经制单！";
			}
			// 使用Jackson包来处理json
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(rs);
			return json;
		}
}
